package com.aca.impl;

import com.aca.dao.AdminDAO;
import com.aca.entity.Admin;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Repository
public class AdminDAOImpl extends HibernateDaoSupport implements AdminDAO {

    @Override
    public void regAdmin(Admin admin) {
        getHibernateTemplate().save(admin);
    }

    @Override
    public void delAdmin(Admin admin) {
        getHibernateTemplate().delete(admin);
    }

    @Override
    public void delAdmin(String userName) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        List<Admin> list = null;
        Query query = null;
        String hql = "from Admin where username=?1";
        query = session1.createQuery(hql);
        list = query.setParameter(1,userName).list();

        transaction.commit();
        session1.close();
        session1 = null;

        Admin tmp = list.get(0);
        getHibernateTemplate().delete(tmp);

    }

    @Override
    public Admin findAdminByName(String name, boolean isUsername) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        List<Admin> list = null;
        Query query = null;
        String hql = "";

        //同理 分username和nickname查找
        if (isUsername){
            hql = "from Admin where username=?1";
            query = session1.createQuery(hql);
            list = query.setParameter(1,name).list();
        }else{
            hql = "from Admin where nickname=?1";
            query = session1.createQuery(hql);
            list = query.setParameter(1,name).list();
        }

        transaction.commit();
        session1.close();
        session1 = null;

        //判空
        if (list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }

    @Override
    public Admin findAdminByUserName(String userName) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        String hql = "from Admin where username=?1";
        Query query = session1.createQuery(hql);
        List<Admin> list = query.setParameter(1,userName).list();

        transaction.commit();
        session1.close();
        session1 = null;

        if (list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }

    @Override
    public Admin findAdminByNickName(String nickName) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        String hql = "from Admin where nickname=?1";
        Query query = session1.createQuery(hql);
        List<Admin> list = query.setParameter(1,nickName).list();

        transaction.commit();
        session1.close();
        session1 = null;

        if (list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }

    @Override
    public List pageAdmin(int pageCur, int pageSize, Admin admin) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        String hql = "";
        if (admin != null){
            hql = "from Admin where nickname like '%" + admin.getNickname().trim()
                    + "%' order by nickname";
        }else {
            return null;
        }
        Query query = session1.createQuery(hql);
        int firstRes = (pageCur - 1) * pageSize;
        query.setFirstResult(firstRes);
        query.setMaxResults(pageSize);
        List list = query.list();
        transaction.commit();
        session1.close();
        session1 = null;
        return list;
    }

    @Override
    public int getAdminTol() {
        return findAllAdmin().size();
    }

    @Override
    public List findAllAdmin() {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        String hql = "from Admin";
        Query query = session1.createQuery(hql);
        List<Admin> list = query.list();

        transaction.commit();
        session1.close();
        session1 = null;

        return list;
    }

    @Override
    public boolean existAdmin(String name, boolean isUsername) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        List<Admin> list = null;
        Query query = null;
        String hql = "";
        //同理 分username和nickname查找
        if (isUsername){
            hql = "from Admin where username=?1";
            query = session1.createQuery(hql);
            list = query.setParameter(1,name).list();
        }else{
            hql = "from Admin where nickname=?1";
            query = session1.createQuery(hql);
            list = query.setParameter(1,name).list();
        }

        transaction.commit();
        session1.close();
        session1 = null;

        if (list.size() > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean existAdminByUserName(String userName) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        String hql = "from Admin where username=?1";
        Query query = session1.createQuery(hql);
        List<Admin> list = query.setParameter(1,userName).list();

        transaction.commit();
        session1.close();
        session1 = null;

        if (list.size()>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean existAdminByNickName(String nickName) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        String hql = "from Admin where nickname=?1";
        Query query = session1.createQuery(hql);
        List<Admin> list = query.setParameter(1,nickName).list();

        transaction.commit();
        session1.close();
        session1 = null;

        if (list.size()>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Admin checkAdmin(String username, String password) {
        Session session1 = getHibernateTemplate().getSessionFactory().openSession();
        Transaction transaction = session1.beginTransaction();
        //Query parameters are numerated starting with 1
        String hql = "from Admin where username=?1 and pwd=?2";
        Query query = session1.createQuery(hql);
        List<Admin> list = query
                .setParameter(1,username)
                .setParameter(2,password)
                .list();
        if (list.size() > 0){
            return list.get(0);
        }else {
            return null;
        }


    }
}
